﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Evan.Model;

namespace Evan.DataBase
{
    /// <summary>
    /// 数据操作接口
    /// </summary>
    public interface IDbOperat
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbOperat"></param>
        /// <param name="tabName"></param>
        /// <returns></returns>
        DbOperat SetTable(DbOperat dbOperat, string tabName);


        #region GetModel 方法

        /// <summary>
        /// 获取当前实例对象后的某个表中的所有字段 通过ID
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        dynamic GetModel(string TableName, int Id);

        /// <summary>
        /// 获取当前实例对象后的指定表中的指定字段 通过ID
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Id"></param>
        /// <param name="Fields"></param>
        /// <returns></returns>
        dynamic GetModelSomeField(string TableName, int Id, string Fields);

        /// <summary>
        /// 通过Where 获取单条数据
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Where">where 语句  注意 : 不用写where</param>
        /// <param name="param">参数列表</param>
        /// <returns></returns>
        dynamic GetModel(string TableName, string Where, params object[] param);
        /// <summary>
        /// 通过Where 获取单条数据
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Fields">字段名 Id,Title</param>
        /// <param name="Where">where 语句  注意 : 不用写where</param>
        /// <param name="param">参数列表</param>
        /// <returns></returns>
        dynamic GetModelSomeField(string TableName, string Fields, string Where, params object[] param);


        #endregion

        #region GetList 方法

        /// <summary>
        /// 获取列表方法
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Where">条件</param>
        /// <returns></returns>
        List<dynamic> GetList(string TableName, string Where);

        /// <summary>
        /// 获取列表方法
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Top">前几条</param>
        /// <param name="Where">条件</param>
        /// <returns></returns>
        List<dynamic> GetList(string TableName, int Top, string Where);

        /// <summary>
        /// 获取列表方法
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Where">条件</param>
        /// <param name="Order">排序</param>
        /// <returns></returns>
        List<dynamic> GetList(string TableName, string Where, string Order);

        /// <summary>
        /// 获取列表方法
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Top">前几条</param>
        /// <param name="Where">条件</param>
        /// <param name="Order">排序</param>
        /// <param name="param">参数化语句的参数</param>
        /// <returns></returns>
        List<dynamic> GetList(string TableName, int Top, string Where, string Order, params object[] param);


        /// <summary>
        /// 获取列表方法
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Top">前几条</param>
        /// <param name="Fields">那些字段</param>
        /// <param name="Where">条件</param>
        /// <param name="Order">排序</param>
        /// <param name="param">参数化语句的参数</param>
        /// <returns></returns>
        List<dynamic> GetListSomeField(string TableName, int Top, string Fields, string Where, string Order, params object[] param);



        #endregion

        #region GetPage 方法 结果中包含总共多少条


        /// <summary>
        /// 获取Page Page不从0开始计算  从1开始计算
        /// 增加总条数参数
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="PageSize">页大小 最低为1</param>
        /// <param name="Where">where</param>
        /// <param name="CurPage">当前页 最低为1</param>
        /// <param name="OrderBy">排序默认使用 ID DESC (ID倒序排列)</param>
        /// <param name="IgnoreReleaseTime">是否忽略定时发布 , 用于后台 , true 为忽略定时发布条件 false 可以查找出所有的数据(包括未发布的数据)</param> 
        /// <returns></returns>
        SysModel.PageModels GetPage(string TableName, int PageSize, int CurPage, string Where, string OrderBy, bool IgnoreReleaseTime = true);

        /// <summary>
        /// 获取Page Page不从0开始计算  从1开始计算
        /// 增加总条数参数
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Fields">要取的数据库字段</param>
        /// <param name="PageSize">页大小 最低为1</param>
        /// <param name="CurPage">当前页 最低为1</param>
        /// <param name="Where">条件</param>
        /// <param name="OrderBy">排序默认使用 ID DESC (ID倒序排列)</param>
        /// <param name="IgnoreReleaseTime">是否忽略定时发布 , 用于后台 , true 为忽略定时发布条件 false 可以查找出所有的数据(包括未发布的数据)</param> 
        /// <returns></returns>
        SysModel.PageModels GetPage(string TableName, string Fields, int PageSize, int CurPage, string Where, string OrderBy, bool IgnoreReleaseTime = true);




        #endregion

        #region GetSearchPage 方法 获取搜索分页数据


        /// <summary>
        /// 获取搜索分页
        /// 获取Page Page不从0开始计算  从1开始计算
        /// 增加总条数参数
        /// </summary>
        /// <param name="PageSize">页大小 最低为1</param>
        /// <param name="CurPage">当前页 最低为1</param>
        /// <param name="OrderBy">排序默认使用 ID DESC (ID倒序排列)</param>
        /// <returns></returns>
        SysModel.PageModels GetSearchPage(string TableName, int PageSize, int CurPage, string Where, string OrderBy);

        /// <summary>
        /// 获取搜索分页
        /// 获取Page Page不从0开始计算  从1开始计算
        /// 增加总条数参数
        /// </summary>
        /// <param name="Fields">要取的数据库字段</param>
        /// <param name="PageSize">页大小 最低为1</param>
        /// <param name="CurPage">当前页 最低为1</param>
        /// <param name="Where">条件</param>
        /// <param name="OrderBy">排序默认使用 ID DESC (ID倒序排列)</param>
        /// <returns></returns>
        SysModel.PageModels GetSearchPage(string TableName, string Fields, int PageSize, int CurPage, string Where, string OrderBy);




        #endregion

        #region Insert 方法

        /// <summary>
        /// 新增一条数据 返回新增数据后的Model
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        dynamic InsertReturnModel(string TableName, dynamic Model);


        /// <summary>
        /// 插入一条数据 返回新增数据的ID
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns></returns>
        int InsertReturnId(string TableName, dynamic Model);



        #endregion

        #region Update 方法

        /// <summary>
        /// 更新字段  注意SQL字段和参数必须顺序正确
        /// </summary>
        /// <param name="Fields">字段 Title=@Title</param>
        /// <param name="Where">条件 Id=@Id</param>
        /// <param name="param">"TItle",Id</param>
        /// <returns></returns>
        bool UpdateFields(string TableName, string Fields, string Where, params object[] param);

        /// <summary>
        /// 根据Id更新一个Model
        /// </summary>
        /// <param name="TableName">表名称</param>
        /// <param name="Model">动态Model</param>
        /// <returns></returns>
        bool Update(string TableName, dynamic Model);

        #endregion

        #region Delete 方法

        /// <summary>
        /// 数据库删除
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Where"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        bool Delete(string TableName, string Where, params object[] param);

        /// <summary>
        /// 虚拟删除  [Status]=-1
        /// </summary>
        /// <param name="Where"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        bool Delete_Virtual(string TableName, string Where, params object[] param);
        /// <summary>
        /// 虚拟删除  [Status]=-1
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        bool Delete_Virtual(string TableName, int Id, params object[] param);
        #endregion

        #region 获取上下一条的方法
        /// <summary>
        /// 获取上一条数据的方法
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        dynamic GetPrev(string TableName, dynamic Model, string Order, params object[] param);


        /// <summary>
        /// 获取吓一跳数据的方法
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        dynamic GetNext(string TableName, dynamic Model, string Order, params object[] param);


        #endregion


        #region 事务操作
        /// <summary>
        /// 事务操作 , 传入的SQL语句至少需要一条
        /// </summary>
        /// <param name="SqlStrList"></param>
        /// <returns></returns>
        bool ExecuteTransaction(List<string> SqlStrList);
        #endregion


        #region 复制数据
        /// <summary>
        /// 复制一条数据 . 被复制的表如果没有字段会报错   注意字段大小写区别 
        /// </summary>
        /// <param name="TableName">表名称</param>
        /// <param name="Id">标识列</param>
        /// <param name="CusFields">如果有表是需要自己设置字段的需要单独处理</param>
        /// <returns></returns>
        int CopyData(string TableName, string Id, Dictionary<string, string> CusFields);

        /// <summary>
        /// 复制字段表多条数据 . 被复制的表如果没有字段会报错   注意字段大小写区别 
        /// </summary>
        /// <param name="TableName">表名称</param>
        /// <param name="ClassId">栏目表的ID</param>
        /// <param name="CusFields">需要替换的字段值 key=字段 value=值</param>
        /// <returns></returns>
        bool CopyChannelFields(string TableName, string ClassId, Dictionary<string, string> CusFields);

        /// <summary>
        /// 复制栏目里面的内容 
        /// <para>复制栏目的时候选择了复制数据,  则可以将被复制的栏目里面的数据也都Copy过来</para>
        /// </summary>
        /// <param name="TableName">栏目内容所属表</param>
        /// <param name="ClassId">栏目ClassId</param>
        /// <param name="CusFields">自定义需要复制的字段</param>
        /// <returns></returns>
        bool CopyChannelData(string TableName, string ClassId, Dictionary<string, string> CusFields);

        /// <summary>
        /// 复制模板表多条数据 . 被复制的表如果没有字段会报错   注意字段大小写区别 
        /// <para>复制模板的时候需要复制模板对应的Model</para>
        /// </summary>
        /// <param name="TableName">表名称</param>
        /// <param name="ClassId">栏目表的ID</param>
        /// <param name="CusFields"></param>
        /// <returns></returns>
        bool CopyChannelModules(string TableName, string ClassId, Dictionary<string, string> CusModules);

        #endregion

        #region 验证表或者数据库是否存在
        /// <summary>
        /// IsExistTable 表是否存在 
        /// </summary>
        /// <param name="TableName">表名称</param>
        /// <returns>是否存在 true 存在  false 不存在</returns>
        bool IsExistTable(string TableName);
        /// <summary>
        /// IsExistTable 表是否存在 
        /// </summary>
        /// <param name="DataBase"> 链接字符串 为空标示使用实例化当前DbOperat的数据库</param>
        /// <param name="TableName">表名称</param>
        /// <returns>是否存在 true 存在  false 不存在</returns>
        bool IsExistTable(string DataBase, string TableName);


        /// <summary>
        /// 检查表中是否有当前字段 使用当前实例的数据库
        /// </summary>
        /// <param name="TableName">表名称</param>
        /// <param name="Field">字段名称</param>
        /// <returns>是否存在 true 存在  false 不存在</returns>
        bool IsExistField(string TableName, string Field);
        /// <summary>
        /// 检查表中是否有当前字段
        /// </summary>
        /// <param name="DataBase">数据库链接字符串</param>
        /// <param name="TableName">表名称</param>
        /// <param name="Field">字段名称</param>
        /// <returns>是否存在 true 存在  false 不存在</returns>
        bool IsExistField(string DataBase, string TableName, string Field);
        #endregion

        #region 数据库字段操作

        /// <summary>
        /// 添加字段到默认数据库的指定表
        /// </summary>
        /// <param name="FieldModel"></param>
        /// <returns></returns>
        bool AddFieldToTable(SysModel.FieldModel FieldModel);

        /// <summary>
        /// 添加字段到指定数据库的指定表
        /// </summary>
        /// <param name="DataBase"></param>
        /// <param name="FieldModel"></param>
        /// <returns></returns>
        bool AddFieldToTable(string DataBase, SysModel.FieldModel FieldModel);


        #endregion

        #region 数据库备份

        /// <summary>
        /// 数据库备份
        /// 备份为脚本文件
        /// </summary>
        /// <param name="connName">链接字符串名称</param>
        /// <param name="filePath">备份路径(站点根目录)</param>
        /// <returns></returns>
        bool BackUpDataBase(string connName = "", string filePath = "");

        #endregion

        #region 直接执行SQL语句
        /// <summary>
        /// 执行SQL语句 返回List
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        DataTable ExecSqlCmd_Table(string sql, params object[] param);

        /// <summary>
        /// 执行SQL语句 返回单行数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        DataRow ExecSqlCmd_Row(string sql, params object[] param);

        /// <summary>
        /// 执行SQL语句 返回第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        object ExecSqlCmd_Object(string sql, params object[] param);

        /// <summary>
        /// 执行SQL语句 返回是否执行成功
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        int ExecSqlCmd_NonQuery(string sql, params object[] param);
        #endregion

        #region 直接执行存储过程
        /// <summary>
        /// 执行SQL存储过程 返回List
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        DataTable ExecSqlProceducre_Table(string sql, params object[] param);

        /// <summary>
        /// 执行SQL存储过程 返回单行数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        DataRow ExecSqlProceducre_Row(string sql, params object[] param);

        /// <summary>
        /// 执行SQL存储过程 返回第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        object ExecSqlProceducre_Object(string sql, params object[] param);

        /// <summary>
        /// 执行SQL存储过程 返回是否执行成功
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        int ExecSqlProceducre_NonQuery(string sql, params object[] param);
        #endregion

        /// <summary>
        /// 日志信息
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">异常信息</param>
        void Debug(string title, dynamic message);




    }
}
